// 导入api
import memberApi from "@/api/device";
import { mapGetters } from 'vuex';
import VueElementLoading from 'vue-element-loading';
import { hasPermissionPoint } from "@/utils/permission";
import devicedataApi from "@/api/device";

const components = {
    VueElementLoading
};

const data = function () {
    return {
        list: [],
        total: 0, //总记录数
        currentPage: 1, //当前页码
        pageSize: 10, // 每页显示10条数据
        radio: null, // 如果使用单选框，定义一个model值
        currentSelectItem: {} //当前选中的值
    };
};

const methods = {
    btnshow(point) {
        return hasPermissionPoint(point);
    },
    chooseone(row) {
        this.radio = row.id;
    },
    handleSelectionChange(row) {
        console.log(row);
        this.currentSelectItem = row;
    },
    // 当每页显示条数改变后，被触发，val是最新的显示条数
    handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.fetchData();
    },
    // 当页码改变后，被触发，val是最新的页码
    handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.fetchData();
    },
    fetchData() {
        // memberApi.getList().then(response => {
        //   const resp = response.data;
        //   this.list = resp.data;
        //   console.log(resp);
        // });
        var vm = this.$store;
        vm.commit('updateLoading', true);
        console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading);
        // 调用分页查询
        memberApi
            .searchDeviceData(
                this.currentPage,
                this.pageSize,
                this.$store.state.devicedata.tSerialNumber
            )
            .then(resp => {
                // const resp = response.data;
                this.list = resp.data.rows;
                this.total = resp.data.total;
                if (resp.data) {
                    vm.commit('updateLoading', false);
                    console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
                }
                console.log(resp);
            });
    },
    // handleEdit(id) {
    //   console.log("编辑", id);
    // },
    handleDelete(id) {
        if (id != null && id != "") {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    devicedataApi.deleteDeviceData(id).then(resp => {
                        if (resp.data.code == 0) {
                            this.$message({
                                message: "删除数据成功",
                                type: "success"
                            });
                            this.fetchData();
                            this.radio = "";
                        } else {
                            this.$message({
                                message: "删除数据失败",
                                type: "warning"
                            });
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        } else {
            this.$message({
                message: "请选择需要删除的数据哦！",
                type: "warning"
            });
        }
    }
};

const watch = {
    $route(to, from) {
        this.fetchData();
    }
};

const computed = {
    ...mapGetters([
        'name',
        'roles'
    ])
};

function created() {
    // 初始化获取列表数据
    this.fetchData();
};

export default {
    components,
    data,
    methods,
    created,
    watch,
    computed
}